package com.zhiqianbao.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.zhiqianbao.R;
import com.zhiqianbao.entity.Repay;
import com.zhiqianbao.utils.Constants;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by liang on 2016/1/14.
 */
public class RepayConfirmRbActivity extends BaseActivity {
    private static String TAG = RepayConfirmRbActivity.class.getSimpleName();
    WebView mWebView;

    Repay repay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repay_confirm_rb);
        init();
        initView();
    }

    private void initView() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        mWebView=(WebView)findViewById(R.id.mWebView);

        Intent intent = getIntent();
        String mapDate = (String) intent.getSerializableExtra("params");
        Map<String, String> params = gson.fromJson(mapDate, HashMap.class);

        String queryString = "";

        for (String key : params.keySet()) {
            queryString += "&" + key + "=" + params.get(key);
        }

        queryString = "?" + queryString.substring(1);

        Log.i(TAG, "queryString = " + queryString);

        WebSettings webSettings = mWebView.getSettings();
        //设置WebView属性，能够执行Javascript脚本
        webSettings.setJavaScriptEnabled(true);
        mWebView.addJavascriptInterface(this, "repayRb");

        //设置可以访问文件
        webSettings.setAllowFileAccess(false);
        //设置支持缩放
        webSettings.setBuiltInZoomControls(false);
        //支持内容重新布局  
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        //加载需要显示的网页
        String usrs=Constants.API_ROOT + "/reapal/pay" + queryString;
        mWebView.loadUrl(Constants.API_ROOT + "/reapal/pay" + queryString);
        //设置Web视图
        mWebView.setWebViewClient(new WebViewClient());

        mWebView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Log.i(TAG, "当前URL ： " + mWebView.getUrl());
                return true;
            }
        });

        mWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            };

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "当前URL ： " + url);
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == event.KEYCODE_BACK) {

            String url = mWebView.getUrl();
            if (url.indexOf("result") != -1) {
                return super.onKeyDown(keyCode, event);
            }

            if (mWebView.canGoBack()) {
                mWebView.goBack();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @JavascriptInterface
    public void closeActivity(){
        showShortToast1("支付成功");
        finish();
    }
}
